/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jun 8, 2012  1:12:52 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : AccountPayableVoucherServices.java
 * TODO
 */
package jpa.service.promis.construction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.construction.AccountPayableVoucher;
import jpa.entitys.promis.construction.AccountPayableVoucherPK;
import jpa.entitys.promis.construction.ProjectAccountCost;
import jpa.service.common.JPAService;

import org.apache.log4j.Logger;

import promis.utility.CommonService;

/**
 * @author chatree
 *
 */
public class AccountPayableVoucherServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(AccountPayableVoucherServices.class);

	/**
	 * @param em
	 */
	public AccountPayableVoucherServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	public AccountPayableVoucher findById(AccountPayableVoucherPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "AccountPayableVoucher.findById"));				
	}
	
	public List<AccountPayableVoucher> findBySapLivDocNo(String company, String project, String syncExternalRefNo, String status) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("syncExternalRefNo", syncExternalRefNo);
		mapWhereValue.put("status", status);
		
		return super.ConvertToYoureObject(super.find("AccountPayableVoucher.findBySapLivDocNo", mapWhereValue));						
	}
	
	public AccountPayableVoucher InsertedOrUpdated(AccountPayableVoucher vlaue) throws Exception {
		AccountPayableVoucher obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{					
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public AccountPayableVoucher Updated(AccountPayableVoucher vlaue) throws Exception {
		AccountPayableVoucher obj = vlaue;
		try{
			
			if(vlaue.getId() != null)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}				
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<AccountPayableVoucher> Inserted(List<AccountPayableVoucher> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(AccountPayableVoucher obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<AccountPayableVoucher> Updated(List<AccountPayableVoucher> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(AccountPayableVoucher obj : vlauelist)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				common_lst.add(obj);
			}
			
			if(common_lst.size() == vlauelist.size())
				super.updated(common_lst);		
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}	
	
	public void SaveH_D(AccountPayableVoucher accountPayableVoucher, List<ProjectAccountCost> ProjectAccountCost_List, boolean isNew)throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			List<CommonEntity> common_lst1 = new ArrayList<CommonEntity>();
			
			if(accountPayableVoucher != null)
				common_lst.add(accountPayableVoucher);
			else
				 throw new Exception("Exception :: AccountPayableVoucher is null. ");
			
			if(CommonService.getSize(ProjectAccountCost_List) > 0){
				for(ProjectAccountCost obj1 : ProjectAccountCost_List)
					common_lst1.add(obj1);	
			}
					
			SaveH_D(common_lst,common_lst1, isNew);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
	}
	
	private void SaveH_D(List<CommonEntity> AccountPayableVoucher_List, List<CommonEntity> ProjectAccountCost_List, boolean isNew) throws Exception{
		EntityTransaction t = null;
		try{			
	        t = getEm().getTransaction();
	        t.begin();
	        if(isNew){
	        	//Insert
	        	for(CommonEntity object : AccountPayableVoucher_List)
		        	getEm().persist(object);
		        
		        		for(CommonEntity object : ProjectAccountCost_List)
		    	        	getEm().persist(object);
	        }else{
	        	//Update
	        	for(CommonEntity object : AccountPayableVoucher_List)
		        	getEm().merge(object);
	        	
	        	if(CommonService.getSize(ProjectAccountCost_List) > 0){
	        		for(CommonEntity object : ProjectAccountCost_List)
	        			getEm().merge(object);	
	        	}		        
	        }
		}catch(Exception e)
		{
			e.printStackTrace();
			t.rollback();
			throw e;
		}finally{
			t.commit();				
		}
	}

}
